/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FRMAlmacen.java
 *
 * Created on 12/10/2012, 02:06:54 AM
 */

package Vistas;

import Beans.Almacen;
import Beans.Seccion;
import Main.Main;
import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author el Kike 
 */
public class FRMAlmacen extends JDialog {

    /** Creates new form FRMAlmacen */
    Almacen almacen; // dato mienbro
    FRMListaAlmacen padre;
    ArrayList<Seccion> secciones;
    SeccionTableModel seccionTableModel;
    
    public FRMAlmacen(java.awt.Frame parent, boolean modal,Almacen almacen) {
        super(parent, modal);
        initComponents();
        
        seccionTableModel = new SeccionTableModel();
        this.jTable1.setModel(seccionTableModel);
        
        if(almacen==null){
            secciones = new  ArrayList<Seccion>();
        }
        else
        {
           this.almacen=almacen;

           
            this.txtId.setText(""+almacen.getId() );
            this.txtDireccion.setText(almacen.getDireccion() );
            this.txtArea.setText(""+almacen.getArea() );
            
           secciones=Main.servicioSeccion.getSecciones(almacen);
           
        }
        
    }
    class SeccionTableModel extends AbstractTableModel{
        
        String [] nombreColumna = {"Código", "Tipo Articulo","Tipo Almacen", "Estado " };
        
        @Override
        public int getRowCount() {
            return secciones.size();
        }

        @Override
        public int getColumnCount() {
            return 4;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Seccion seccion  =  secciones.get(rowIndex);
            switch(columnIndex){
                
                case 0: return "" + seccion.getId();
                case 1: return  Main.tipoArticulo[ seccion.getTipoArticulo() ] ;
                case 2: return  Main.tipoAlmacenamiento[seccion.getTipoAlmacenamiento()];
                case 3: return  Main.estadosSeccion[seccion.getEstado()];
               

            }
            return null;
        }
        @Override
        public String getColumnName(int columna){
            return nombreColumna[columna];
        }
        
        
    }
    
    public void actualizarTabla(){
       // secciones = Main.servicioArticulo.getArticulos();
        seccionTableModel.fireTableChanged(null);
    } 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnAceptar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txtId = new javax.swing.JTextField();
        txtDireccion = new javax.swing.JTextField();
        txtArea = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setTitle("Almacen");

        jLabel1.setText("ID");

        jLabel2.setText("Direccion");

        jLabel3.setText("Area de almacen");

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");

        jButton3.setText("Agregar seccion");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDireccion)
                            .addComponent(txtArea)
                            .addComponent(txtId)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnAceptar)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)))
                .addGap(42, 42, 42))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        
        FRMSeccion frmSeccion =new FRMSeccion(null,true,null);
        frmSeccion.setPadre2(this);
        frmSeccion.setVisible(true);
        frmSeccion.getSeccion();
        this.secciones.add(frmSeccion.getSeccion());
        
        //actualizar el jtablemodel
    }//GEN-LAST:event_jButton3ActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        
       if(this.almacen==null) 
       {

           almacen = new Almacen();    
           almacen.setId( Integer.parseInt( this.txtId.getText() ) );
           almacen.setArea(  this.txtArea.getText()  );
           almacen.setDireccion(this.txtDireccion.getText());
           
           System.out.print("secciones antes de ok "+secciones.size());
           almacen.setSecciones(secciones);
           
           Main.servicioAlmacen.agregarAlmacen(almacen);
           
       }
       
       // cerramos ventana
       this.dispose();
       
       
       
       
    }//GEN-LAST:event_btnAceptarActionPerformed
    public void setPadre(FRMListaAlmacen padre){
        this.padre=padre;
    
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtArea;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtId;
    // End of variables declaration//GEN-END:variables

}
